package com.sjr.easy.chat.app.controller;

import com.sjr.easy.chat.app.dto.AppTokenInfo;
import com.sjr.easy.chat.app.utils.AppTokenManager;
import com.sjr.easy.chat.dto.Result;
import com.sjr.easy.chat.model.OutboxMessage;
import com.sjr.easy.chat.service.OutboxMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api("用户接收消息相关接口")
@RestController
public class AppOutboxMessageController {

    @Autowired
    private OutboxMessageService outboxMessageService;

    @Autowired
    private AppTokenManager appTokenManager;

    @ApiOperation("查询用户新消息")
    @GetMapping("/app/outboxMessage/getNewOutboxMessage")
    public Result<List<OutboxMessage>> getNewOutboxMessageList() {
        AppTokenInfo tokenInfo = appTokenManager.getTokenInfo();
        List<OutboxMessage> outboxMessageList = outboxMessageService.getNewOutboxMessageList(tokenInfo.getId(), 500);
        return Result.success(outboxMessageList);
    }

    @Data
    static class BatchACKOutboxMessageReq {
        private List<Long> outboxMessageIdList;
    }

    @ApiOperation("批量对用户消息进行ack处理")
    @PostMapping("/app/outboxMessage/batchACKOutboxMessage")
    public Result<Object> batchACKOutboxMessage(@RequestBody BatchACKOutboxMessageReq req) {
        AppTokenInfo tokenInfo = appTokenManager.getTokenInfo();
        outboxMessageService.batchACKOutboxMessage(tokenInfo.getId(), req.outboxMessageIdList);
        return Result.success();
    }
}
